<?php
namespace Admin\Controller;
use Think\Controller;

class PublicController extends Controller {
	public function login($username = null, $password = null, $verify = null) {
		if (IS_POST) {
            /* 检测验证码 TODO: */
            if(!check_verify($verify)){
                $this->error('验证码输入错误！');
            }

            if($username==null || $password==null){
            	$this->error('所有输入项都不能为空^_^');
            }
            $Member=M('Members');
            $map['username'] = $username;
            $useruid = $Member->where($map)->field('uid,userpass,nickname,username,create_time,usergroup')->find();

			if(!$useruid){
				$this->error('用户不存在^_^');
			}else {
				if ($useruid["userpass"]!=md5($password)){
					$this->error('密码错误^_^');
				}else{
					//用户登录成功
            		session('admin',$useruid);
            		session('admin_auth_sign',authcode("$useruid[userpass]\t$useruid[uid]",'ENCODE'));

					$data = array(
						'create_time' => time(),
						'last_login_time' =>$useruid["create_time"],
						'last_login_ip' => get_client_ip(),
					);
					M('Members')->where("uid=".$useruid["uid"])->save($data);
					$this->success("成功登入！",U('Admin/Index/index'));
				}
			}
		}else{
			$this->assign('webtitle',C('web_title')); 
			$this->display();
		}
	}
	

    public function verify(){
        $verify = new \Com\Verify();
        $verify->entry(1);
    }
	
	/**
	* 用户退出
	*/  
	public function logout(){
		session('admin_key',null);
		session_destroy();
		$this->redirect(U('Public/login'));
	}
}